package com.tebiecloud.mpweixin.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.mpweixin.client.model.entity.MpminiUser;
import com.tebiecloud.mpweixin.server.service.MpminiUserService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 小程序用户 前端控制器
 * @author tebie
 * @date 2019-09-24
 */
@RestController
@RequestMapping("/mpminiuser")
public class MpminiUserController {

    @Autowired
    private MpminiUserService mpminiUserService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<MpminiUser>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        MpminiUser query = pageParams.mapToObject(MpminiUser.class);
        QueryWrapper<MpminiUser> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(mpminiUserService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<MpminiUser> get(@RequestParam("id") Long id) {
        MpminiUser entity = mpminiUserService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param openId 用户的标识，对当前公众号唯一,
     * @param nickName 用户的昵称,
     * @param gender 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知,
     * @param avatarUrl 用户头像,
     * @param unionId 只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。,
     * @param phone 小程序手机号,
     * @param country 国家,
     * @param province 省份,
     * @param city 城市,
     * @param lastVisit 最后访问时间
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "openId", required = false) String openId,
        @RequestParam(value = "nickName", required = false) String nickName,
        @RequestParam(value = "gender", required = false) Integer gender,
        @RequestParam(value = "avatarUrl", required = false) String avatarUrl,
        @RequestParam(value = "unionId", required = false) String unionId,
        @RequestParam(value = "phone", required = false) String phone,
        @RequestParam(value = "country", required = false) String country,
        @RequestParam(value = "province", required = false) String province,
        @RequestParam(value = "city", required = false) String city,
        @RequestParam(value = "lastVisit", required = false) Date lastVisit
    ) {
        MpminiUser entity = new MpminiUser();
        entity.setOpenId(openId);
        entity.setNickName(nickName);
        entity.setGender(gender);
        entity.setAvatarUrl(avatarUrl);
        entity.setUnionId(unionId);
        entity.setPhone(phone);
        entity.setCountry(country);
        entity.setProvince(province);
        entity.setCity(city);
        entity.setLastVisit(lastVisit);
        mpminiUserService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param openId 用户的标识，对当前公众号唯一
     * @param nickName 用户的昵称
     * @param gender 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
     * @param avatarUrl 用户头像
     * @param unionId 只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。
     * @param phone 小程序手机号
     * @param country 国家
     * @param province 省份
     * @param city 城市
     * @param lastVisit 最后访问时间
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "mpminiUserId", required = false) Long mpminiUserId,
            @RequestParam(value = "openId", required = false) String openId,
            @RequestParam(value = "nickName", required = false) String nickName,
            @RequestParam(value = "gender", required = false) Integer gender,
            @RequestParam(value = "avatarUrl", required = false) String avatarUrl,
            @RequestParam(value = "unionId", required = false) String unionId,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "country", required = false) String country,
            @RequestParam(value = "province", required = false) String province,
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "lastVisit", required = false) Date lastVisit
    ) {
        MpminiUser entity = new MpminiUser();
        entity.setMpminiUserId(mpminiUserId);
        entity.setOpenId(openId);
        entity.setNickName(nickName);
        entity.setGender(gender);
        entity.setAvatarUrl(avatarUrl);
        entity.setUnionId(unionId);
        entity.setPhone(phone);
        entity.setCountry(country);
        entity.setProvince(province);
        entity.setCity(city);
        entity.setLastVisit(lastVisit);
        mpminiUserService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            mpminiUserService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            mpminiUserService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
